<?php
/**
 * Created by Aliyun ApsaraVideo VoD.
 * User: https://www.aliyun.com/product/vod
 * API document: https://www.alibabacloud.com/help/zh/doc-detail/55407.htm
 */
require_once './VodUploadSDK-PHP_1.0.2/VodUploadSDK-PHP_1.0.2'. DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
date_default_timezone_set('PRC');
// 测试上传本地视频
function testUploadLocalVideo($accessKeyId, $accessKeySecret, $filePath,$videoTitle='testUploadLocalVideo via PHP-SDK')
{
	try {
		$uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
		$uploadVideoRequest = new UploadVideoRequest($filePath, $videoTitle);
		$uploadVideoRequest->setCateId(1000039900);
		$uploadVideoRequest->setCoverURL("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=287611701,4158894526&fm=26&gp=0.jpg");
		//$uploadVideoRequest->setTags('test1,test2');
		//$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
		//$uploadVideoRequest->setTemplateGroupId('6ae347b0140181ad371d197ebe289326');
		$userData = array(
				"MessageCallback"=>array("CallbackURL"=>"https://demo.sample.com/ProcessMessageCallback"),
				"Extend"=>array("localId"=>"xxx", "test"=>"www")
		);
		$uploadVideoRequest->setUserData(json_encode($userData));
		$res = $uploader->uploadLocalVideo($uploadVideoRequest);
		print_r($res);
	} catch (Exception $e) {
		printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
				$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
	}
}
// 测试上传网络视频
function testUploadWebVideo($accessKeyId, $accessKeySecret, $fileURL,$videoWebTitle='testUploadWebVideo via PHP-SDK')
{
	try {
		$uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
		$uploadVideoRequest = new UploadVideoRequest($fileURL, $videoWebTitle);
		$res = $uploader->uploadWebVideo($uploadVideoRequest);
		print_r($res);
	} catch (Exception $e) {
		printf("testUploadWebVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
				$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
	}
}
// 测试上传本地m3u8视频
function testUploadLocalM3u8($accessKeyId, $accessKeySecret, $m3u8FilePath)
{
	try {
		$uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
		$uploadVideoRequest = new UploadVideoRequest($m3u8FilePath, 'testUploadLocalM3u8 via PHP-SDK');
		// 调用接口解析m3u8的分片地址列表，如果解析结果不准确，请自行拼接地址列表(默认分片文件和m3u8文件位于同一目录)
		$sliceFiles = $uploader->parseM3u8File($m3u8FilePath);
		//print_r($sliceFiles);
		$res = $uploader->uploadLocalM3u8($uploadVideoRequest, $sliceFiles);
		print_r($res);
	} catch (Exception $e) {
		printf("testUploadLocalM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
				$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
	}
}
// 测试上传网络m3u8视频
function testUploadWebM3u8($accessKeyId, $accessKeySecret, $m3u8FileUrl)
{
	try {
		$uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
		$uploadVideoRequest = new UploadVideoRequest($m3u8FileUrl, 'testUploadWebM3u8 via PHP-SDK');
		// 调用接口解析m3u8的分片地址列表，如果解析结果不准确，请自行拼接地址列表(默认分片文件和m3u8文件位于同一目录)
		$sliceFileUrls = $uploader->parseM3u8File($m3u8FileUrl);
		//print_r($sliceFileUrls);
		$res = $uploader->uploadWebM3u8($uploadVideoRequest, $sliceFileUrls);
		print_r($res);
	} catch (Exception $e) {
		printf("testUploadWebM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
				$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
	}
}
####执行本地上传测试代码####
$accessKeyId = '<AccessKeyId>';
$accessKeySecret = '<AccessKeySecret>';
$localFilePath = 'd:\Pictures\Camera Roll\60917d3913a8e33b3af62b402e29f03a.mp4';
//$localFilePath = '/opt/video/sample.mp4';
//$webFileURL = 'http://outin-eac0cded752311e9be0800163e024c6a.oss-cn-shanghai.aliyuncs.com/60917d3913a8e33b3af62b402e29f03a.mp4?auth_key=1608774986-0-0-c56acd302bea0c331370d8ed686502fe';
//$videoTitle = 'vd0002-daizhizi';
//testUploadLocalVideo($accessKeyId, $accessKeySecret, $localFilePath,$videoTitle);

$localM3u8FilePath = '/opt/video/m3u8/sample.m3u8';
//testUploadLocalM3u8($accessKeyId, $accessKeySecret, $localM3u8FilePath);
$webM3u8FileURL = 'http://vod-test1.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc95c8/daa30814c0c340cf8199926f78aa5c0e-195a25af366b5edae324c47e99a03f04-ld.m3u8?auth_key=1608775606-0-0-9fb038deaecd009dadd86721c5855629';
//testUploadWebM3u8($accessKeyId, $accessKeySecret, $webM3u8FileURL);

####执行网络图片上传测试代码####
####upload-fail...
$webFileURL = "https://outin-eac0cded752311e9be0800163e024c6a.oss-cn-shanghai.aliyuncs.com/sv/4c482b26-16aaef50531/4c482b26-16aaef50531.mp4?Expires=1557728246&OSSAccessKeyId=LTAIxSaOfEzCnBOj&Signature=vpF%2BYzZv2Tr2UcM%2FB2P%2BukC9760%3D";
$videoWebTitle = 'vd0003';
testUploadWebVideo($accessKeyId, $accessKeySecret, $webFileURL,$videoWebTitle);